using System;
using System.Collections.Generic;
using System.Text;

namespace WebControls
{
   public class SFNumericBox:SFTextBox, IValidation
    {

       public SFNumericBox()
       {
           this.Attributes.Add("onkeypress", "return validateNumericBox();");
           this.Attributes.Add("onkeyup", "FormatNum(this);");
       }

       #region Members
       private long _MinValue;
       private long _MaxValue;
       private bool _FormatNumberOnClient = true;

       #endregion

       #region Properties
       
       public long MinValue
       {
           get
           {
               return _MinValue;
           }
           set
           {
               _MinValue = value;
           }
       }

     
       public long MaxValue
       {
           get
           {
               return _MaxValue;
           }
           set
           {
               _MaxValue = value;
           }
       }

   
       public new bool FormatNumberOnClient
       {
           get { return _FormatNumberOnClient; }
           set { _FormatNumberOnClient = value; }
       }
       #endregion

       protected override void OnLoad(EventArgs e)
       {
           base.OnLoad(e);
       }

       public override bool Validate()
       {
           base.Validate();
           if (!this.HasErrors)
           {
               if (!Validation.ValidateNumber(this.Text))
               {
                   this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(19, new object[] { StaticMethods.GetErrorMessageControlName(this) });
               }
           }
           if (!this.HasErrors && this.Text != "")
           {
               int val;
               if (!int.TryParse(this.Text, out val))
               {
                 
                   this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(20, new object[] { StaticMethods.GetErrorMessageControlName(this), int.MaxValue.ToString() });
               }
           }
           if (!this.HasErrors)
           {
               if (_MinValue == 0 && _MinValue != _MaxValue && this.Text != "")
               {
                   if (!Validation.ValidateAmongNumbers(this.Text, _MinValue, _MaxValue))
                   {
                     
                       this.ErrorMessage += StaticMethods.GetErrorMessageMultilanguageString(21, new object[] { StaticMethods.GetErrorMessageControlName(this), _MinValue, _MaxValue });
                   }
               }
           }

           return this.HasErrors;
       }

       public override string Text
       {
           get
           {
               return base.Text.Replace(".", "");
           }
           set
           {
               if (value == null)
                   base.Text = "";
               else
                   base.Text = System.String.Format(value, "#,#");
           }
       }
       public new int? Value
       {
           get
           {
               if (this.Text == "")
               {
                   return null;
               }
               else
               {
                   return Convert.ToInt32(this.Text);
               }
           }
           set
           {
               if (value.HasValue)
               {
                   this.Text = value.ToString();
               }
               else
               {
                   this.Text = "";
               }
           }
       }
      
    }
}
